package com.lx.yygh.hosp.api;

import com.lx.yygh.common.exception.YyghException;
import com.lx.yygh.common.helper.HttpRequestHelper;
import com.lx.yygh.common.result.Result;
import com.lx.yygh.common.result.ResultCodeEnum;
import com.lx.yygh.common.util.MD5;
import com.lx.yygh.hosp.service.DepartmentService;
import com.lx.yygh.hosp.service.HospitalService;
import com.lx.yygh.hosp.service.HospitalSetService;
import com.lx.yygh.hosp.service.ScheduleService;
import com.lx.yygh.model.hosp.Department;
import com.lx.yygh.model.hosp.Hospital;
import com.lx.yygh.model.hosp.Schedule;
import com.lx.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author c先生
 * @date 2022年10月15日 下午 5:52:48
 * @describe
 */
@Api(tags = "医院管理API的接口")
@RestController
@RequestMapping("/api/hosp")
public class HospitalApiController {
    @Resource
    private HospitalService hospitalService;
    @Resource
    private HospitalSetService hospitalSetService;
    @Resource
    private ScheduleService scheduleService;
    @Resource
    private DepartmentService departmentService;


    @ApiOperation(value = "上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取传递数据的hoscode
        String hoscode = (String) map.get("hoscode");
        //判断hoscode的值是否为空
        if(!StringUtils.hasText(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //根据hoscode获取签名（未加密状态）
        String signKey = hospitalSetService.getSignKey(hoscode);
        //首先对签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //校验签名
        if(!map.get("sign").equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //请求过程中，字符出现了  “+“转化为 ” “ ，所以需要转化回来
        String logoData = (String) map.get("logoData");
        if(StringUtils.hasText(logoData)){
           String newlogoData = logoData.replaceAll(" ","+");
            map.put("logoData",newlogoData);
        }

        Boolean save = hospitalService.saveHospital(map);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "查询医院")
    @PostMapping("/hospital/show")
    public Result findHospital(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取传递数据的hoscode
        String hoscode = (String) map.get("hoscode");
        //判断hoscode的值是否为空
        if(!StringUtils.hasText(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //根据hoscode获取签名（未加密状态）
        String signKey = hospitalSetService.getSignKey(hoscode);

        //首先对签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //校验签名
        if(!map.get("sign").equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospitalByHoscode = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospitalByHoscode);
    }

    @ApiOperation(value = "上传排班信息")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        Boolean save = scheduleService.saveSchedule(map);
        return Result.ok();
    }

    @ApiOperation(value = "查询排班信息")
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取传递数据的hoscode
        String hoscode = (String) map.get("hoscode");
        //判断hoscode的值是否为空
        if(!StringUtils.hasText(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //根据hoscode获取签名（未加密状态）
        String signKey = hospitalSetService.getSignKey(hoscode);
        //校验签名
        if(!HttpRequestHelper.isSignEquals(map,signKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String pageStr = (String) map.get("page");
        Integer page = Integer.parseInt(pageStr);
        String limitStr = (String) map.get("limit");
        Integer limit = Integer.parseInt(limitStr);
        Schedule schedule = new Schedule();
        Page pageModel = scheduleService.findPageSchedule(page,limit,schedule);
        return Result.ok(pageModel);
    }

    @ApiOperation("删除排班信息")
    @PostMapping("/schedule/remove")
    public Result deleteSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取传递数据的hoscode
        String hoscode = (String) map.get("hoscode");
        //判断hoscode的值是否为空
        if(!StringUtils.hasText(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //根据hoscode获取签名（未加密状态）
        String signKey = hospitalSetService.getSignKey(hoscode);
        //校验签名
        if(!HttpRequestHelper.isSignEquals(map,signKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String hosScheduleId = (String) map.get("hosScheduleId");
        Boolean aBoolean = scheduleService.deleteSchedule(hosScheduleId);
        if(aBoolean){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "分页查询所有科室")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取传递数据的hoscode
        String hoscode = (String) map.get("hoscode");
        //判断hoscode的值是否为空
        if(!StringUtils.hasText(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //根据hoscode获取签名（未加密状态）
        String signKey = hospitalSetService.getSignKey(hoscode);
        //首先对签名进行md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //校验签名
        if(!map.get("sign").equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String pageStr = (String) map.get("page");
        Integer page = Integer.parseInt(pageStr);
        String limitStr = (String) map.get("limit");
        Integer limit = Integer.parseInt(limitStr);
        Department department = new Department();
        Page pageModel = departmentService.findPageDepartment(page,limit,department);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "增加科室")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        Boolean save = departmentService.saveDepartment(map);
        return Result.ok();
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("/department/remove")
    public Result deleteDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //获取传递数据的hoscode
        String hoscode = (String) map.get("hoscode");
        //判断hoscode的值是否为空
        if(!StringUtils.hasText(hoscode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //根据hoscode获取签名（未加密状态）
        String signKey = hospitalSetService.getSignKey(hoscode);
        //校验签名
        if(!HttpRequestHelper.isSignEquals(map,signKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String depcode = (String) map.get("depcode");
        Boolean aBoolean = departmentService.deleteDepartment(depcode);
        if(aBoolean){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}
